package com.lw.service.impl;

import com.baidu.aip.util.Base64Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.mapper.UserMapper;
import com.lw.service.UserService;
import com.lw.system.Role;
import com.lw.system.User;
import com.lw.utils.BaiduAiUtil;
import com.lw.utils.QiniuUploadUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BaiduAiUtil baiduAiUtil;
    @Override
    public Page<User> findAll(Map<String,Object> map) {
         /*
            根据查询条件查询用户
          1.hasDept是否分配部门 0未分配 1已分配
          2.departmentId通过部门id查询
          3.companyId通过公司部门查询
        */
        QueryWrapper<User> wrapper = new QueryWrapper<>(  );
        if (map.get( "departmentId" )!=null){
            wrapper.eq( "department_id",map.get( "departmentId" ));
        }
        if (map.get( "companyId" )!=null){
            wrapper.eq( "company_id",map.get( "companyId" ));
        }
        if ("0".equals(map.get( "hasDept" ))){
            wrapper.isNull( "department_id" );
        }
        if ("1".equals(map.get( "hasDept" ))){
            wrapper.isNotNull( "department_id" );
        }
        int current = Integer.parseInt( (String) map.get( "current" ) );
        int size = Integer.parseInt( (String) map.get( "size" ) );
        Page<User> page = new Page<>(current,size);
        Page<User> users = userMapper.findAll( page,wrapper );
        return users;
    }

    @Override
    public List<Role> findOtherRoles(String id) {
        return userMapper.findOtherRoles( id );
    }

    @Override
    public void assignRoles(Map<String, Object> map) {
        List<String> roleIds = (List<String>) map.get( "roleIds" );
        for (String id:roleIds){
            userMapper.addRole((String) map.get( "id" ),id );
        }
    }

    @Override
    public User findById(String id) {
        return userMapper.findById( id );
    }

    @Override
    public void uploadImg(String id, MultipartFile file) throws IOException {
        //先查在更
        User user = userMapper.findById( id );
      /*  //使用dataUrl形式储存图片
        String encode ="data:image/jpeg;base64,"+  Base64.encodeBase64String( file.getBytes() ) ;
        user.setStaffPhoto( encode );*/
        //使用七牛云存储    同时存储到百度云人脸库中         用用户id作为图片名称 前端需要重新获取用户属性
        String imgUrl = new QiniuUploadUtil().upload( user.getId(), file.getBytes() );
        user.setStaffPhoto( imgUrl );
        userMapper.updateById( user );
        //判断是否已经注册面部信息
        Boolean aBoolean = baiduAiUtil.faceExist( id );
        if (aBoolean){
            //更新
            baiduAiUtil.faceUpdate( id,Base64.encodeBase64String( file.getBytes() ) );
        }else {
            //注册
            baiduAiUtil.faceRegister( id,Base64.encodeBase64String( file.getBytes() ) );
        }
    }
}
